// stores/auth.js
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import { getPermissions } from '../api/auth'
import type { Menu } from '../types/menu'

export const useAuthStore = defineStore('auth', () => {
  const menus = ref<Menu[]>([]);
  
  // 初始化用户菜单权限
  const initMenus = async () => {
    const cached = localStorage.getItem('permissions');
    if (cached) {
      menus.value = JSON.parse(cached)
    } else {
        menus.value = await getPermissions()
    }
  };
  
  // 检查是否有某菜单权限
  const hasMenu = computed(() => (code:string) => {
    return menus.value.some(menu => menu.code === code);
  });

  return { menus, initMenus, hasMenu };
});